package com.lazybones.base.engineer.common.conf.jasypt;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableEncryptableProperties
public class JasyptConfig {
    private final Logger log = LoggerFactory.getLogger(JasyptConfig.class);
    @Value("${jasypt.encryptor.password}")
    private String encryptorPass;
    @Value("${jasypt.encryptor.algorithm}")
    private String algorithm;
    @Value("${jasypt.encryptor.iv-generator-classname}")
    private String ivClassName;
    @Value("${jasypt.encryptor.salt-generator-classname}")
    private String slatName;
    @Value("${jasypt.encryptor.pool-size}")
    private Integer poolSize;
    @Value("${jasypt.encryptor.key-obtention-iterations}")
    private Integer keyObtention;
    @Value("${jasypt.encryptor.provider-name}")
    private String providerName;
    @Value("${jasypt.encryptor.string-output-type}")
    private String outPutType;

    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(encryptorPass);
        config.setAlgorithm(algorithm);
        config.setKeyObtentionIterations(keyObtention);
        config.setPoolSize(poolSize);
        config.setProviderName(providerName);
        config.setSaltGeneratorClassName(slatName);
        config.setIvGeneratorClassName(ivClassName);
        config.setStringOutputType(outPutType);
        encryptor.setConfig(config);
        log.info("poolSize value is  {}",poolSize);
        return encryptor;
    }
    public String encryptorPass(String text){
        String encrypt = new JasyptConfig().stringEncryptor().encrypt(text);
        return encrypt;
    }
}
